#!/bin/sh
# sourced by /init..
# sets PLANG,   PKEYS, VFAT_OUT_PARAM
#      FONTMAP, KMAP,  CODEPAGE

# functions called by init 
plang_pupstate() {
 if [ "$PLANG" ]; then #seems like a good thing!
   echo "#PLANG is written to LANG in /etc/profile by init script initrd...
PLANG=${PLANG}
OUTPUT_CHARSET=UTF-8
export OUTPUT_CHARSET"
 fi
}
plang_copy_to_newroot() {
 [ -d "/pup_new/lib/consolefonts" ] || mkdir -p /pup_new/lib/consolefonts
 cp -a /lib/consolefonts/* /pup_new/lib/consolefonts/
 if [ "$KMAP" ];then #because PKEYS boot param was defined.
   echo -n "$KMAP" > /pup_new/etc/keymap
   echo -n "$FONTMAP" > /pup_new/etc/fontmap
   echo -n "$CODEPAGE" > /pup_new/etc/codepage
 fi
 # if PLANG set here, carry it over...
 [ "$PLANG" ] && sed -i "s%^LANG=.*%LANG=${PLANG}%" /pup_new/etc/profile
}

#--------------------------------------------------------------------------
# code block that is immediately executed (. /sbin/set_plang)

[ $plang ] && PLANG=$plang #boot parameter
[ "`echo -n "$PLANG" | grep '_'`" = "" ] && PLANG="" #PLANG must be complete, ex: de_DE.UTF-8.
[ ! "$PLANG" ] && [ -s /PUPPYLANG ] && PLANG=`cat /PUPPYLANG` #so Woof can specify a default lang.

[ $pkeys ] && PKEYS=$pkeys #boot parameter, keyboard layout w476

#120216 /PUPPYKEYMAP inserted in initrd by quicksetup (in future, by Woof too)...
FONTMAP=""
[ ! "$PKEYS" ] && [ -s /PUPPYKEYMAP ] && PKEYS="`cat /PUPPYKEYMAP`" #allow kernel boot param to override.

if [ "$PLANG" ];then
 if [ ! "$PKEYS" ];then
  #try to set PKEYS to match the language. first 2 letters of PLANG...
  PKEYS=${PLANG:0:2} #rough as guts, assign first 2 chars of PLANG to PKEYS.
  case $PLANG in
   en_GB*) PKEYS=uk ;;
   en*) PKEYS=us ;;
   ja*) PKEYS=jp106 ;;
  esac
 fi
 #120216 L18L suggests load these, instead of what is below...
 case $PLANG in
  en*) echo -n ;;
  ar*|iw*) #L18L no Greek
   setfont /lib/consolefonts/LatArCyrHeb-16.psfu.gz -C /dev/tty1
   FONTMAP='LatArCyrHeb-16.psfu'
  ;;
  ru*) #vkvkvk for ru
   zcat /lib/consolefonts/ter-u16n.psf.gz | loadfont
   FONTMAP='ter-u16n.psf'
  ;;
  *) #L18L All European languages; new default ?!
   zcat /lib/consolefonts/LatGrkCyr-8x16.psfu.gz | loadfont
   FONTMAP='LatGrkCyr-8x16.psfu'
  ;;
 esac
 if [ -f /locale/${PLANG%.*}/init.mo ];then
   . /locale/${PLANG%.*}/init.mo
 elif [ -f /locale/${PLANG%_*}/init.mo ];then
   . /locale/${PLANG%_*}/init.mo
 fi
fi

#091122 load keyboard layout if PKEYS boot param...
STATUS=0
CODEPAGE=""
KMAP=""

if [ "$PKEYS" ];then
 case $PKEYS in
   en) PKEYS=us ;;
   gb) PKEYS=uk ;;
   ja|jp) PKEYS=jp106 ;;
 esac
 if [ ! -f /lib/keymaps/${PKEYS}.gz ];then
  PKEYS="`ls -1 /lib/keymaps/${PKEYS}*.gz | head -n 1 | rev | cut -f 1 -d '/' | cut -f 2 -d '.' | rev`"
 fi
 if [ -f /lib/keymaps/${PKEYS}.gz ];then
  echo -n "$(printf "${L_LOADING_KEYBOARD_LAYOUT}" "$PKEYS")" >/dev/console
  KMAP="$PKEYS"
  zcat /lib/keymaps/${PKEYS}.gz | loadkmap ; STATUS=$(($STATUS + $?))
  case $PKEYS in #note, same code in /etc/rc.d/rc.country, /usr/sbin/input-wizard and init.
   de*|be*|br*|dk*|es*|fi*|fr*|it*|no*|se*|pt*)
    [ ! "$PLANG" ] && FONTMAP="lat1-12.psfu" #120216
    CODEPAGE="850"
    VFAT_OUT_PARAM="$OUT_PARAM"',codepage=850'
   ;;
   cz*|hu*|pl*|ro*|sk*|croat*|slovene*)
    [ ! "$PLANG" ] && FONTMAP="lat2-12.psfu" #120216
    CODEPAGE="852"
    VFAT_OUT_PARAM="$OUT_PARAM"',codepage=852,iocharset=iso8859-2'
   ;;
  esac
  if [ ! "$PLANG" ];then #120216 old behaviour.
   if [ "$FONTMAP" ];then #100520 fix syntax error...
    zcat /lib/consolefonts/${FONTMAP}.gz | loadfont
    STATUS=$(($STATUS + $?))
   fi
  fi
  check_status $STATUS
 fi
fi

### END ###